/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.server.modules.sys.web.loan;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.server.common.config.Global;
import com.server.common.web.BaseController;
import com.server.common.utils.StringUtils;
import com.server.modules.sys.entity.loan.LoanType;
import com.server.modules.sys.service.loan.LoanTypeService;

/**
 * 贷款类型表Controller
 * @author 王磊
 * @version 2016-09-13
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/loan/loanType")
public class LoanTypeController extends BaseController {

	@Autowired
	private LoanTypeService loanTypeService;
	
	@ModelAttribute
	public LoanType get(@RequestParam(required=false) String id) {
		LoanType entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = loanTypeService.get(id);
		}
		if (entity == null){
			entity = new LoanType();
		}
		return entity;
	}
	
	@RequiresPermissions("sys:loan:loanType:view")
	@RequestMapping(value = {"list", ""})
	public String list(LoanType loanType, HttpServletRequest request, HttpServletResponse response, Model model) {
		List<LoanType> list = loanTypeService.findList(loanType); 
		model.addAttribute("list", list);
		return "modules/sys/loan/loanTypeList";
	}

	@RequiresPermissions("sys:loan:loanType:view")
	@RequestMapping(value = "form")
	public String form(LoanType loanType, Model model) {
		if (loanType.getParent()!=null && StringUtils.isNotBlank(loanType.getParent().getId())){
			loanType.setParent(loanTypeService.get(loanType.getParent().getId()));
			// 获取排序号，最末节点排序号+30
			if (StringUtils.isBlank(loanType.getId())){
				LoanType loanTypeChild = new LoanType();
				loanTypeChild.setParent(new LoanType(loanType.getParent().getId()));
				List<LoanType> list = loanTypeService.findList(loanType); 
				if (list.size() > 0){
					loanType.setSort(list.get(list.size()-1).getSort());
					if (loanType.getSort() != null){
						loanType.setSort(loanType.getSort() + 30);
					}
				}
			}
		}
		if (loanType.getSort() == null){
			loanType.setSort(30);
		}
		model.addAttribute("loanType", loanType);
		return "modules/sys/loan/loanTypeForm";
	}

	@RequiresPermissions("sys:loan:loanType:edit")
	@RequestMapping(value = "save")
	public String save(LoanType loanType, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, loanType)){
			return form(loanType, model);
		}
		String url = StringUtils.getFirstUrl(loanType.getIconUrl());//过滤竖线
		loanType.setIconUrl(url);
		loanTypeService.save(loanType);
		addMessage(redirectAttributes, "保存贷款类型成功");
		return "redirect:"+Global.getAdminPath()+"/sys/loan/loanType/?repage";
	}
	
	@RequiresPermissions("sys:loan:loanType:edit")
	@RequestMapping(value = "delete")
	public String delete(LoanType loanType, RedirectAttributes redirectAttributes) {
		loanTypeService.delete(loanType);
		addMessage(redirectAttributes, "删除贷款类型成功");
		return "redirect:"+Global.getAdminPath()+"/sys/loan/loanType/?repage";
	}

	@RequiresPermissions("user")
	@ResponseBody
	@RequestMapping(value = "treeData")
	public List<Map<String, Object>> treeData(@RequestParam(required=false) String extId, HttpServletResponse response) {
		List<Map<String, Object>> mapList = Lists.newArrayList();
		List<LoanType> list = loanTypeService.findList(new LoanType());
		for (int i=0; i<list.size(); i++){
			LoanType e = list.get(i);
			if (StringUtils.isBlank(extId) || (extId!=null && !extId.equals(e.getId()) && e.getParentIds().indexOf(","+extId+",")==-1)){
				Map<String, Object> map = Maps.newHashMap();
				map.put("id", e.getId());
				map.put("pId", e.getParentId());
				map.put("name", e.getName());
				mapList.add(map);
			}
		}
		return mapList;
	}
	
}